﻿// -----------------------------------------------------------------------
// <copyright file="PathLinkListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db PathLinkList
    /// </summary>
    public class PathLinkListData
    {
        /// <summary>
        /// Get PathLinkList By Id
        /// </summary>
        /// <param name="pathLinkListId">Param pathLinkListId</param>
        /// <returns>Return PathLinkList</returns>
        public PathLinkListEN GetPathLinkListById(int pathLinkListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                PathLinkListEN pathLinkList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePathLinkListGetById, pathLinkListId);
                while (reader.Read())
                {
                    pathLinkList = new PathLinkListEN();

                    if (!reader.IsDBNull(0))
                    {
                        pathLinkList.PathLinkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        pathLinkList.Name = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        pathLinkList.Path = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        pathLinkList.Descriptions = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return pathLinkList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all PathLinkList
        /// </summary>
        /// <returns>Return list all PathLinkList</returns>
        public IList<PathLinkListEN> GetPathLinkLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<PathLinkListEN> lstPathLinkList = new List<PathLinkListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePathLinkListGetAll);
                while (reader.Read())
                {
                    PathLinkListEN pathLinkList = new PathLinkListEN();

                    if (!reader.IsDBNull(0))
                    {
                        pathLinkList.PathLinkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        pathLinkList.Name = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        pathLinkList.Path = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        pathLinkList.Descriptions = reader.GetString(3);
                    }

                    lstPathLinkList.Add(pathLinkList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstPathLinkList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db PathLinkList
        /// </summary>
        /// <param name="pathLinkList">Paarm PathLinkList</param>
        /// <returns>Insert PathLinkList</returns>
        public int Insert(PathLinkListEN pathLinkList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.Name, pathLinkList.Name),
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.Path, pathLinkList.Path),
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.Descriptions, pathLinkList.Descriptions)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePathLinkListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db PathLinkList
        /// </summary>
        /// <param name="pathLinkList">Param PathLinkList</param>
        public void Update(PathLinkListEN pathLinkList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.PathLinkId, pathLinkList.PathLinkId),
                                   new SqlParameter(Constants.SqlParamerterName.PathLinkList.Name, pathLinkList.Name),
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.Path, pathLinkList.Path),
                                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.Descriptions, pathLinkList.Descriptions)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePathLinkListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete PathLinkList by PathLinkId
        /// </summary>
        /// <param name="pathLinkId">param pathLinkId</param>
        public void Delete(int pathLinkId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.PathLinkList.PathLinkId, pathLinkId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePathLinkListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}